package com.saint.farmshop.Mapper.MapperImpl;

import com.saint.farmshop.Entity.Production;
import com.saint.farmshop.Entity.ProductionDetail;
import com.saint.farmshop.Mapper.ProductionMapper;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 产品映射器实现类
 *
 * @author Saint
 * @date 2022/05/23
 */
@Repository
public class ProductionMapperImpl implements ProductionMapper {

    @Resource
    MongoTemplate mongoTemplate;

    final String COLLECTION_NAME = "production_list";

    final String DETAIL_COLLECTION_NAME = "production_detail_list";

    @Override
    public List<Production> getProductionByType(String typeId, Integer pageIndex, Integer pageSize) {
        System.out.println(typeId);
        Criteria criteria = Criteria.where("typeId").is(typeId).and("status").is(1);
        return getProductions(pageIndex, pageSize, criteria);
    }

    @Override
    public List<Production> getProductionByOpenId(String openId, Integer pageIndex, Integer pageSize) {
        Criteria criteria = Criteria.where("openId").is(openId);
        return getProductions(pageIndex, pageSize, criteria);
    }

    @Override
    public Boolean releaseProduction(ProductionDetail productionDetail) {
        productionDetail.setStatus(1);
        productionDetail.setUpdateTime(LocalDateTime.now());
        if(mongoTemplate.insert(productionDetail,DETAIL_COLLECTION_NAME) != null){
            Production production = new Production(productionDetail);
            production.setId(production.getId());
            mongoTemplate.insert(production,COLLECTION_NAME);
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public Boolean deleteProduction(ProductionDetail productionDetail) {
        Criteria criteria = Criteria.where("id").is(productionDetail.getId()).and("status").is(1);
        Query query = new Query(criteria);
        if(mongoTemplate.findAndRemove(query,ProductionDetail.class,DETAIL_COLLECTION_NAME) != null) {
            if (mongoTemplate.remove(query,Production.class, COLLECTION_NAME) != null) {
                return true;
            } else {
                return false;
            }
        }
        else{
            return false;
        }
    }

    @Override
    public ProductionDetail getProductionDetailById(String id) {
        Criteria criteria = Criteria.where("id").is(id);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query,ProductionDetail.class,DETAIL_COLLECTION_NAME);
    }

    @Override
    public List<Production> getProductionList(Integer pageIndex, Integer pageSize) {
        Criteria criteria = Criteria.where("status").is(1);
        List<Production> productions = getProductions(pageIndex,pageSize,criteria);
        return productions;
    }

    @Override
    public boolean updateProductionStatus(String productionId) {
        Update update = new Update().set("status",0);
        Criteria criteria = Criteria.where("id").is(productionId);
        Query query = new Query(criteria);
        if(mongoTemplate.updateFirst(query,update,Production.class,COLLECTION_NAME) != null){
            if(mongoTemplate.updateFirst(query,update,ProductionDetail.class,DETAIL_COLLECTION_NAME) != null){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

    private List<Production> getProductions(Integer pageIndex, Integer pageSize, Criteria criteria) {
        Query query = new Query(criteria);
        int skip = (pageIndex - 1) * pageSize;
        query.skip(skip);
        query.limit(pageSize);
        List<Production> productions = mongoTemplate.find(query, Production.class, COLLECTION_NAME);
        return productions;
    }

}
